package com.base.library.view.galleryViewPager;

import android.content.Context;
import android.view.ViewGroup;

import com.base.library.view.galleryImageView.UrlTouchImageView;

import java.util.List;

/**
 * 作者：王东一 on 2015/8/6 13:37
 */
public class UrlPagerAdapter extends BasePagerAdapter {

    private UrlTouchImageView view;

    public UrlPagerAdapter(Context context, List<String> resources) {
        super(context, resources);
    }

    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        super.setPrimaryItem(container, position, object);
        ((GalleryViewPager) container).mCurrentView = ((UrlTouchImageView) object).getImageView();
        this.view = ((UrlTouchImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        final UrlTouchImageView iv = new UrlTouchImageView(mContext);
        iv.setUrl(mResources.get(position));
        iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        collection.addView(iv, 0);
        return iv;
    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        super.destroyItem(collection, position, view);
        this.view = (UrlTouchImageView) view;
    }

    public void clean() {
        if (view.mImageLoader != null)
            view.mImageLoader.clearMemoryCache();
        if (view.mAttacher != null)
            view.mAttacher.cleanup();
    }
}

